/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.service.services.references;

import opiam.admin.faare.config.javabeans.JBBaseReferenceListDescriptor;
import opiam.admin.faare.config.javabeans.JBSearchReferenceListDescriptor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * This class is used to define the references list such as defined in the
 * references_conf.xml file.
 */
public class ReferenceList
{
    /** Application scope. */
    public static final String SCOPE_APPLICATION = "application";

    /** Session scope. */
    public static final String SCOPE_SESSION = "session";

    /** Request scope. */
    public static final String SCOPE_REQUEST = "request";

    /** Identifier, should be case-insensitive. */
    private String name;

    /** The main store of the references list. */
    private List referenceList = new ArrayList();

    /** Set of references, the map allows to make the access easier to a
     * reference by his label (mapKey). */
    private Map referenceMap = null;

    /** Scope by default = session. */
    private String scope = SCOPE_SESSION;

    /**
     * Creates a new ReferenceList object.
     *
     * @param attrList  Attributes list.
     * @param desc  Descriptor of the references list.
     */
    public ReferenceList(List attrList, JBBaseReferenceListDescriptor desc)
    {
        scope = desc.getScope();
        name = desc.getName();

        Iterator iter = attrList.iterator();
        ReferenceElement elt = null;
        String str = null;

        while (iter.hasNext())
        {
            str = (String) iter.next();
            elt = new ReferenceElement(str, desc.isToParse());
            referenceList.add(elt);
        }
    }

    /**
     * Creates a new ReferenceList object.
     *
     * @param desc Descriptor of the references list.
     */
    public ReferenceList(JBSearchReferenceListDescriptor desc)
    {
        scope = desc.getScope();
        name = desc.getName();
    }

    /**
     * Returns the name of the references list.
     *
     * @return The name.
     */
    public String getName()
    {
        return name;
    }

    /**
     * Sets the name of the references list.
     *
     * @param aname  The name to set.
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**
     * Gets the refernces list as an elements list of
     * opiam.admin.faare.service.services.references.ReferenceElement type.
     *
     * @return The references list.
     */
    public List getReferenceList()
    {
        return referenceList;
    }

    /**
     * Adds a reference element to the references list.
     *
     * @param ref  The reference element to add.
     */
    public void addReference(ReferenceElement ref)
    {
        referenceList.add(ref);
    }

    /**
     * Gets the references list as a hash table (Java map object).<br>
     * The keys of this table are the labels of the list elements, and the
     * values of the table are the corresponding values of the list elements.
     *
     * @return The map representing the references list.
     */
    public Map getReferenceMap()
    {
        if (referenceMap == null)
        {
            referenceMap = new HashMap();

            Iterator iter = referenceList.iterator();
            ReferenceElement ref = null;

            while (iter.hasNext())
            {
                ref = (ReferenceElement) iter.next();
                referenceMap.put(ref.getLabel(), ref.getValue());
            }
        }

        return referenceMap;
    }

    /**
     * Indicates if the scope value of the reference is APPLICATION.
     *
     * @return True if the scope value is APPLICATION, false otherwise.
     */
    public boolean isScopeApplication()
    {
        return (scope == SCOPE_APPLICATION);
    }

    /**
     * Indicates if the scope value of the reference is SESSION.
     *
     * @return True if the scope value is SESSION, false otherwise.
     */
    public boolean isScopeSession()
    {
        return (scope == SCOPE_SESSION);
    }

    /**
     * Indicates if the scope value of the reference is REQUEST.
     *
     * @return True if the scope value is REQUEST, false otherwise.
     */
    public boolean isScopeRequest()
    {
        return (scope == SCOPE_REQUEST);
    }

    /**
     * Converts the references list into a String.
     *
     * @return The String representing the references list.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = referenceList.iterator();

        ReferenceElement ref = null;
        buf.append("REF LIST ");
        buf.append(name);
        buf.append(" : ");
        buf.append(System.getProperty("line.separator"));

        while (it.hasNext())
        {
            ref = (ReferenceElement) it.next();
            buf.append(ref.toString());
            buf.append(System.getProperty("line.separator"));
        }

        return buf.toString();
    }

    /**
     * Sorts the references list.
     * The caller must verify if the description ( sort attribute) corresponds or not.
     */
    public void sortReference()
    {
        Collections.sort(referenceList);
    }
}
